package in.muton.edushare.newentities;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.wicket.model.IModel;

import com.edushare.common.dao.CoursesDAO;
import com.edushare.common.orm.ORMInterface;
import com.edushare.common.util.StringUtil;
import com.edushare.common.vo.Departments;

public class NewCourseCreator implements Serializable {
	private static final long serialVersionUID = 651843391981372776L;
	private ORMInterface ormInterface;
	private IModel<Departments> dept;

	public NewCourseCreator(ORMInterface ormInterface,
			IModel<Departments> dept) {
		super();
		this.ormInterface = ormInterface;
		this.dept = dept;
	}

	public List<String> addCoursesToDept(String courseNameString) {
		if(StringUtil.isValid(courseNameString)){
			String[] courseNameList = breakString(courseNameString);
			List<String> statusList = new ArrayList<String>();
			for (String courseName : courseNameList) {
				if(!StringUtil.isValid(courseName))
					continue;
				statusList.add(new CoursesDAO(ormInterface).addCourseFB(courseName.trim(),
						dept.getObject()));
			}
			return statusList;
		}else
			return new ArrayList<String>();
	}

	public String[] breakString(String courseList) {
		Pattern p = Pattern.compile(";+");
		courseList = courseList.replace('\n',';').replace('\r', ';');
		Matcher m = p.matcher(courseList);
		return m.replaceAll(";").split(";");
	}
}
